<?php

namespace Home\Controller;

/**
 * 游戏管理-百乐之尊
 *
 * @author Administrator
 *        
 */
class BJLController extends BaseController {
	/**
	 *
	 * @var \Req_sfsBJL
	 */
	private $m_ReqBJL;
	/**
	 *
	 * @var integer
	 */
	private $m_GameID = 5;
	/**
	 */
	function __construct() {
		$this->m_ReqBJL = new \Req_sfsBJL ();
	}
	/**
	 * 获取配置
	 */
	public function mb_GetConfig() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->GetConfig () );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		
		$data = $reqData->data;
		$conn = $data->JTTX;
		$name = $data->FJM;
		$coin = $data->BZ;
		$limit = $data->XH;
		$bet = $data->ZXYF;
		$roundLock = $data->DLBJ;
		$maxEnter = $data->MFS;
		$heLimit = $data->HXZ;
		$minEnter = $data->SFS;
		$baoJi = $data->ZYLBJ;
		$xianZhu = $data->ZGDM;
		$roBotCount = $data->JQR;
		$maxBet = $data->ZDYF;
		
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "id='$this->m_GameID'" )->find ();
		$config = array (
				'game' => $gameData ['status'],
				'conn' => ( string ) $conn,
				'name' => ( string ) $name,
				'coin' => ( string ) $coin,
				'limit' => ( string ) $limit,
				'bet' => ( string ) $bet,
				'roundlock' => ( string ) $roundLock,
				'maxenter' => ( string ) $maxEnter,
				'helimit' => ( string ) $heLimit,
				'minenter' => ( string ) $minEnter,
				'baoji' => ( string ) $baoJi,
				'xianzhu' => ( string ) $xianZhu ,
				'robotcount' => ( string ) $roBotCount,
				'maxbet' => ( string ) $maxBet 
		);
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $config ) );
	}
	/**
	 * 设置配置
	 */
	public function mb_SetConfig() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$game = $data->game;
		$conn = $data->conn;
		$name = $data->name;
		$coin = $data->coin;
		$limit = $data->limit;
		$bet = $data->bet;
		$roundLock = $data->roundlock;
		$maxEnter = $data->maxenter;
		$helimit = $data->helimit;
		$minEnter = $data->minenter;
		$baoJi = $data->baoji;
		$xianZhu = $data->xianzhu;
		
		$robotCount = $data->robotcount;
		$maxBet = $data->maxbet;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		$gameInfo = M ( 'Gameinfo' );
		$gameInfo->save ( array (
				'id' => $this->m_GameID,
				'status' => $game 
		) );
		
		$lobbyAppInfo = M ( 'Appinfo', '', 'DB_LOBBY' );
		$lobbyAppInfo->save ( array (
				'id' => $this->m_GameID,
				'status' => $game 
		) );
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->SetConfig ( $conn, $name, $coin, $limit, $bet, $roundLock, $maxEnter, $helimit, $minEnter, $baoJi, $xianZhu , $robotCount, $maxBet) );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		
		\WMHandler::mb_GetInstance ()->mb_UpdateGameStatus ( $this->m_GameID, $game );
		
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 获取总账
	 */
	public function mb_GetTotalAccount() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->GetTotalAccount () );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		$data = $reqData->data;
		$jtData = $data->JT1 [0];
		$total = array (
				'bet' => ( string ) $jtData->ZY,
				'win' => ( string ) $jtData->ZW,
				'profit' => ( string ) $jtData->ZYL,
				'profitrate' => ( string ) $jtData->YL 
		);
		$m1 = array (
				'bet' => ( string ) $jtData->ZY,
				'win' => ( string ) $jtData->ZW,
				'profit' => ( string ) $jtData->ZYL,
				'profitrate' => ( string ) $jtData->YL 
		);
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( array (
				'total' => $total,
				'm1' => $m1 
		) ) );
	}
	/**
	 * 获取机台详细账目
	 */
	public function mb_GetDetailAccount() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$mId = $data->mid;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->GetDetailAccount () );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		$data = $reqData->data;
		// 16座
		$seatData = $data->FZW;
		// 4门
		$colorData = $data->SZW;
		
		$seatArr = array ();
		for($i = 0; $i < count ( $seatData ); $i ++) {
			$obj = $seatData [$i];
			$tmp = array (
					'bet' => ( string ) $obj->ZY,
					'win' => ( string ) $obj->ZW,
					'profit' => ( string ) $obj->ZYL,
					'profitrate' => ( string ) $obj->YL 
			);
			$seatArr [] = $tmp;
		}
		$colorArr = array ();
		for($i = 0; $i < count ( $colorData ); $i ++) {
			$obj = $colorData [$i];
			$tmp = array (
					'bet' => ( string ) $obj->ZY,
					'win' => ( string ) $obj->ZW,
					'profit' => ( string ) $obj->ZYL,
					'profitrate' => ( string ) $obj->YL 
			);
			$colorArr [] = $tmp;
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( array (
				'seat' => $seatArr,
				'color' => $colorArr 
		) ) );
	}
	/**
	 * 获取压分明细
	 */
	public function mb_GetBetDetail() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$type = $data->type;
		$key = $data->key;
		$start = $data->start;
		$end = $data->end;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->GetBetDetail ( $type, $key, $start, $end ) );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		$data = $reqData->data->YZMS;
		
		$send = array ();
		for($i = 0; $i < count ( $data ); $i ++) {
			$obj = $data [$i];
			$tmp = array (
					'id' => ( string ) $obj->WJID,
					'name' => ( string ) $obj->UID,
					'round' => ( string ) $obj->LS,
					'inning' => ( string ) $obj->JS,
					'zhuang' => ( string ) $obj->Z,
					'he' => ( string ) $obj->H,
					'xian' => ( string ) $obj->X,
					'result' => ( string ) $obj->JG,
					'bet' => ( string ) $obj->YF,
					'win' => ( string ) $obj->WF,
					'point' => ( string ) $obj->JF,
					'date' => ( string ) $obj->RQ 
			);
			$send [] = $tmp;
		}
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $send ) );
	}
	/**
	 * 获取打码账目
	 */
	public function mb_GetDaMaAccount() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->GetDaMaAccount () );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		$data = $reqData->data;
		
		$power = $data->NLZ;
		$used = $data->YSY;
		
		$roundPower = $data->DNLZ;
		$roundUsed = $data->DYSY;
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( array (
				'power' => ( string ) $power,
				'used' => ( string ) $used,
				'roundpower' => ( string ) $roundPower,
				'roundused' => ( string ) $roundUsed 
		) ) );
	}
	/**
	 * 获取牌路信息
	 */
	public function mb_GetPaiLu() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->GetPaiLu () );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		$data = $reqData->data->PL;
		$send = array ();
		for($i = 0; $i < count ( $data ); $i ++) {
			$obj = $data [$i];
			$send [] = ( string ) $obj->PZ;
		}
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $send ) );
	}
	/**
	 * 实时座位
	 */
	public function mb_GetRealTimeSeatData() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->GetRealTimeSeatData () );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		$userInfo = M ( 'Userinfo' );
		$seats = array ();
		
		$data = $reqData->data->SSZW;
		
		$round = ( string ) $reqData->data->JS;
		$inning = ( string ) $reqData->data->LS;
		$time = ( string ) $reqData->data->DJS;
		$users = ( string ) $reqData->data->RS;
		
		$bets = '';
		$profit = '';
		$profitRate = '';
		$betLog = M ( 'Userbetlog' );
		$winLog = M ( 'Userwinlog' );
		
		for($i = 0; $i < count ( $data ); $i ++) {
			$obj = $data [$i];
			$userId = $obj->PID;
			$userCode = '';
			if ($userId != 0) {
				
				if($obj->JQR == false){
					$userData = $userInfo->where ( "id='$userId'" )->find ();
					$userCode = $userData ['code'];
					$userName = $userData ['name'];
					
					$bets = $userData['bets'];
					$wins = $userData['wins'];
					$profit = $bets - $wins;
					if ($bets == 0)
						$profitRate = 0;
					else
						$profitRate = round ( $profit / $bets, 2 );
				} else {
					$userCode = '00';
					$profitRate = $obj->LSYL;
				}
				

			}
			$tmp = array (
					'id' => ( string ) $obj->PID,
					'seatid' => ( string ) $obj->ZW,
					'zhuang' => ( string ) $obj->Z,
					'he' => ( string ) $obj->H,
					'xian' => ( string ) $obj->X,
					'bet' => ( string ) $obj->ZY,
					'point' => ( string ) $obj->JB,
					'code' => ( string ) $userCode,
					'profit' => ( string ) $obj->ZP,
					'profitrate' => ( string ) $profitRate 
			);
			$seats [] = $tmp;
		}
		$send = array (
				'round' => $round,
				'inning' => $inning,
				'time' => $time,
				'users' => $users,
				'seats' => $seats 
		);
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $send ) );
	}
	
	/**
	 * 提交命令
	 */
	public function mb_SubmitCMD() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$cmdID = $data->cmdid;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->SubmitCMD ( $cmdID ) );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 获取网络状态
	 */
	public function mb_GetNetworkStatus() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->GetNetworkStatus () );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		
		$data = $reqData->data;
		$send = array (
				( string ) $data->ZJ,
				( string ) $data->MC,
				( string ) $data->WLZT 
		);
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $send ) );
	}
	/**
	 *
	 * @param unknown $reqData        	
	 * @return \stdClass
	 */
	private function mb_DecodeSFSData($reqData) {
		$data = new \stdClass ();
		if ($reqData == null || $reqData == '') {
			$data->state = false;
			$data->msg = '外部服务器异常，请稍后再试';
			return $data;
		}
		$reqJson = json_decode ( $reqData );
		if (json_last_error () != JSON_ERROR_NONE) {
			$data->state = false;
			$data->msg = '外部服务器异常，请稍后再试';
			return $data;
		}
		$state = $reqJson->state;
		if ($state == false) {
			$data->state = false;
			$data->msg = $reqJson->msg;
			return $data;
		}
		$data->state = true;
		if (isset ( $reqJson->msg ))
			$data->msg = $reqJson->msg;
		$data->data = $reqJson;
		return $data;
	}
}